<template>
  <!-- 表单 -->
  <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
        <!-- <el-option lebel="正式" value="1" />
        <el-option lebel="非正式" value="2" /> -->
        <el-option
          v-for="obj in hireType"
          :key="obj.id"
          :label="obj.value"
          :value="obj.id"
        />

      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="depttNameFocus" />
      <div v-if="showTree" class="tree-box">
        <el-tree :data="treeData" :props="{label: 'name'}" @node-click="hNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button @click="hCancel">取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </el-form-item>
  </el-form>

</template>

<script>
import { getDepartments } from '@/api/department'
import { addEmployee } from '@/api/employees'
// 导入枚举信息
import employeeEnum from '@/constant/employees'
// hireType: [ { id: 1, value: '正式' }, { id: 2, value: '非正式'} ]
import { tranListToTreeData } from '@/utils'

export default {
  name: 'EmpDialog',
  data() {
    return {
      hireType: employeeEnum.hireType,
      showTree: false, // 是否展示tree组件
      treeData: [], // 部门信息，转后的树结构
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: blur },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  //   computed: {
  //     hireType() {
  //       return employeeEnum.hireType
  //     }
  //   },
  created() {
    // this.loadDepartments() // 获取企业的部门列表
  },
  methods: {
    hCancel() { // 点击取消按钮，关闭弹层
      this.$emit('close')
    },
    depttNameFocus() { // 部门 输入框 获取焦点的事件
      this.loadDepartments()
      this.showTree = true
    },
    async doAdd() { // 添加员工 ajax
      try {
        const res = await addEmployee(this.formData)
        console.log(res)
        this.$message.success('添加成功')

        // 通知父组件，关闭弹框，更新数据
        this.$emit('update-employee')
      } catch {
        this.$message.error('添加失败')
      }
    },
    hSubmit() { // 点确定按钮
      // 兜底验证
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.doAdd() // 添加员工
        }
      })
    },
    async loadDepartments() { // 查询企业的部门列表
      const res = await getDepartments()
      //   console.log(res)
      res.data.depts.shift() // 去掉第一个元素
      this.treeData = tranListToTreeData(res.data.depts)
      // this.showTree = true // 数据回来时再显示tree组件
      // console.log(this.treeData)
    },
    // data 会自动获取到参数，它表示当前点击的节点对应的数据
    // 用户在部门结构树上点击
    hNodeClick(data) { // tree节点的点击事件
      // console.log(data)
      // 如果当前部门还有子部门，则不能选中
      if (data.children.length === 0) {
        this.formData.departmentName = data.name // 把当前选中的显示在input框中
      }
      this.showTree = false // 收起tree组件
    },
    resetForm() { // 清空表单值和表单校验
      // 手动恢复表单初始值
      this.formData = {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
      this.$refs.addForm.resetFields()
    }
  }
}
</script>
